package com.ghostbusters.danceathon;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import wiiremotej.ButtonMap;
import wiiremotej.IRSensitivitySettings;
import wiiremotej.WiiRemote;
import wiiremotej.WiiRemoteExtension;
import wiiremotej.WiiRemoteJ;
import wiiremotej.event.WRAccelerationEvent;
import wiiremotej.event.WRButtonEvent;
import wiiremotej.event.WRCombinedEvent;
import wiiremotej.event.WRExtensionEvent;
import wiiremotej.event.WRIREvent;
import wiiremotej.event.WRNunchukExtensionEvent;
import wiiremotej.event.WRStatusEvent;
import wiiremotej.event.WiiRemoteListener;

import com.intel.bluetooth.BlueCoveConfigProperties;

public class WiiRemoteMain implements WiiRemoteListener {
	public WiiRemote remote;
	public static List<IRPoint> coords;
	public static JPanel graph;
	public static JFrame frame;
	public static final int DIMENSIONS = 800;

	public WiiRemoteMain(WiiRemote remote) {
		this.remote = remote;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		WiiRemoteJ.setConsoleLoggingAll();
		System.setProperty(
				BlueCoveConfigProperties.PROPERTY_JSR_82_PSM_MINIMUM_OFF,
				"true");
		try {
			WiiRemote remote = WiiRemoteJ.findRemote();
			remote.addWiiRemoteListener(new WiiRemoteMain(remote));
			remote.setAccelerometerEnabled(true);
			remote.setSpeakerEnabled(true);
			remote.setIRSensorEnabled(true, WRIREvent.BASIC,
					IRSensitivitySettings.WII_LEVEL_4);
			remote.setLEDIlluminated(0, true);
			remote.getButtonMaps().add(
					new ButtonMap(WRButtonEvent.HOME, ButtonMap.NUNCHUK,
							WRNunchukExtensionEvent.C,
							new int[] { java.awt.event.KeyEvent.VK_CONTROL },
							java.awt.event.InputEvent.BUTTON1_MASK, 0, -1));
		} catch (Throwable e) {

		}
		frame = new JFrame();
		frame.setTitle("IR graph");
		frame.setSize(DIMENSIONS, DIMENSIONS);
		frame.setResizable(false);
		graph = new JPanel() {
			public void paintComponent(Graphics g) {
				for (int i = 0; i < coords.size(); i++) {
					g.clearRect(0, 0, DIMENSIONS, DIMENSIONS);
					g.setColor(java.awt.Color.RED);
					g.fillOval((int) (coords.get(i).getX() * DIMENSIONS),
							(int) (coords.get(i).getY() * DIMENSIONS), 6, 6);
				}

			}
		};
		graph.setSize(800, 800);
		frame.add(graph);
		frame.setVisible(true);

	}

	@Override
	public void IRInputReceived(WRIREvent e) {
		coords = new ArrayList<IRPoint>();
		for (int i = 0; i < e.getIRLights().length; i++) {
			if (e.getIRLights()[i] != null) {
				System.out.println("IR: (" + e.getIRLights()[i].getX() + ", "
						+ e.getIRLights()[i].getX() + ")");
				double x = e.getIRLights()[i].getX();
				double y = e.getIRLights()[i].getY();
				coords.add(new IRPoint(x, y));
			}

			// TODO post to server
		}
		if (graph != null)
			graph.repaint();

	}

	@Override
	public void accelerationInputReceived(WRAccelerationEvent e) {

		// debugging code, not important
		// System.out.println("Acceleration: (" + e.getXAcceleration() + ", "
		// + e.getYAcceleration() + ", " + e.getZAcceleration() + ")");

	}

	@Override
	public void buttonInputReceived(WRButtonEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void combinedInputReceived(WRCombinedEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void disconnected() {
		// TODO Auto-generated method stub

	}

	@Override
	public void extensionConnected(WiiRemoteExtension arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void extensionDisconnected(WiiRemoteExtension arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void extensionInputReceived(WRExtensionEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void extensionPartiallyInserted() {
		// TODO Auto-generated method stub

	}

	@Override
	public void extensionUnknown() {
		// TODO Auto-generated method stub

	}

	@Override
	public void statusReported(WRStatusEvent arg0) {
		// TODO Auto-generated method stub

	}

}
